package com.example.testdemo.kinetic.sprite;

import org.andengine.opengl.texture.region.ITiledTextureRegion;

import com.example.testdemo.TankGameActivity;
import com.example.testdemo.kinetic.interfaces.IEnergyStorable;
import com.example.testdemo.kinetic.interfaces.IOnDestroyListener;
import com.example.testdemo.kinetic.interfaces.IOnDestroyTargetListener;
import com.example.testdemo.kinetic.sprite.bullet.Bullet1;
import com.example.testdemo.kstatic.sprite.ItemSprite;
import com.example.testdemo.pools.BulletPools;
import com.example.testdemo.scenes.stage.SpriteTag;

public abstract class TankSprite extends KineticSprite implements
        IEnergyStorable {
	@SuppressWarnings("unused")
	private final String TAG = TankSprite.class.getSimpleName();

	protected String mName = null;

	protected BulletPools mBulletPools = null;

	private SpriteTag mSpriteTag = null;

	protected boolean mIsDestroyed = false;
	private IOnDestroyListener mOnDestroyListener = null;
	protected IOnDestroyTargetListener mOnDestroyTargetListener = null;

	protected boolean mAnimationCancelable = true;

	public TankSprite(int whichScene, String pName, float pX, float pY,
	        ITiledTextureRegion pTiledTextureRegion, TankGameActivity pActivity) {
		super(whichScene, pName, pX, pY, pTiledTextureRegion, pActivity);
		retrieveBulletPools();
		mSpriteTag = mOnScene.getSpriteTag();
	}

	private void retrieveBulletPools() {
		mBulletPools = BulletPools.getInstance(mWhichScene);
	}

	protected abstract void pickItem(ItemSprite sprite);

	protected abstract void walkingAnimation();

	protected abstract void fireAnimation();

	protected abstract void destroyedAnimation(
	        IOnDestroyListener onDestroyListener);

	public void setOnDestroyListener(IOnDestroyListener onDestroyListener) {
		mOnDestroyListener = onDestroyListener;
	}

	public void setOnDestroyTargetListener(
	        IOnDestroyTargetListener onDestroyTargetListener) {
		mOnDestroyTargetListener = onDestroyTargetListener;
	}

	public void fire() {
		final Bullet1 bullet = mBulletPools.getOneBullet(this);
		if (bullet == null) {
			return;
		}

		if (mOnDestroyTargetListener != null) {
			bullet.registerDestroyTargetListener(NAME, mOnDestroyTargetListener);
		}

		fireAnimation();
		bullet.setEnergyLevel(getEnergyLevel());
		bullet.fire(this);

		clearEnergy();
	}

	@Override
	protected void onManagedUpdate(float pSecondsElapsed) {
		super.onManagedUpdate(pSecondsElapsed);
		if (mSpriteTag.ITEM_END - mSpriteTag.ITEM_START > 0) {
			final float COLLISION_DETECT_RANGE = 64f;

			for (int i = mSpriteTag.ITEM_START; i <= mSpriteTag.ITEM_END; i++) {
				ItemSprite item = (ItemSprite) mOnScene.getChildByTag(i);
				if (item == null || !item.isVisible()) {
					continue;
				}
				if (item.getY() - getY() <= COLLISION_DETECT_RANGE) {
					if (item.getX() - getX() <= COLLISION_DETECT_RANGE) {
						if (collidesWith(item)) {
							pickItem(item);
						}
					}
				}
			}
		}
	}

	@Override
	public void setVisible(boolean pVisible) {
		super.setVisible(pVisible);
		if (pVisible) {
			mIsDestroyed = false;
			walkingAnimation();
		}
	}

	public boolean isDestroyed() {
		return mIsDestroyed;
	}

	public void destroyed() {
		mIsDestroyed = true;
		mCharacterResources.mSoundBomb.play();
		destroyedAnimation(mOnDestroyListener);
	}
}
